<?php

/**
 * consulta actions.
 *
 * @package    dentist
 * @subpackage consulta
 * @author     Brus
 */
class consultaActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $usuarioId = $this->getUser()->getAttribute('id');
        $this->Consultas = ConsultaPeer::getConsultas($usuarioId);
    }

    public function executeShow(sfWebRequest $request) {
        $this->Consulta = ConsultaPeer::retrieveByPk($request->getParameter('id'));
        $this->forward404Unless($this->Consulta);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new ConsultaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new ConsultaForm();

        $this->processFormCreate($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($Consulta = ConsultaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object Consulta does not exist (%s).', $request->getParameter('id')));
        $this->form = new ConsultaForm($Consulta);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($Consulta = ConsultaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object Consulta does not exist (%s).', $request->getParameter('id')));
        $this->form = new ConsultaForm($Consulta);

        $this->processFormUpdate($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($Consulta = ConsultaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object Consulta does not exist (%s).', $request->getParameter('id')));
        $Consulta->delete();

        $this->redirect('consulta/index');
    }

    protected function processFormCreate(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        $usuarioId = $this->getUser()->getAttribute('id');

        if ($form->isValid()) {
            $valores = $this->form->getValues();

            $Consulta = new Consulta();
            $Consulta->setPacienteId($valores['paciente_id']);
            $Consulta->setConsultorioId($valores['consultorio_id']);
            $Consulta->setFecha($valores['fecha']);
            $Consulta->setHora($valores['hora']);
            $Consulta->setTrabajo($valores['trabajo']);
            $Consulta->setCosto($valores['costo']);
            $Consulta->setPago($valores['pago']);
            $Consulta->setUsuarioId($usuarioId);
            $Consulta->save();

            $this->redirect('consulta/show?id=' . $Consulta->getId());
        } else {
            $this->getUser()->setFlash("error", "Error al guardar los datos, por favor verifíquelos y vuelva a intentar.");
        }
    }

    protected function processFormUpdate(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $Consulta = $form->save();

            $this->redirect('consulta/show?id=' . $Consulta->getId());
        } else {
            $this->getUser()->setFlash("error", "Error al guardar los datos, por favor verifíquelos y vuelva a intentar.");
        }
    }

}
